package com.dscs;

import cn.hutool.core.io.IoUtil;
import com.google.code.kaptcha.BackgroundProducer;
import com.google.code.kaptcha.GimpyEngine;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.text.WordRenderer;
import com.google.code.kaptcha.util.Config;
import com.google.code.kaptcha.util.Configurable;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ImageNumCode extends Configurable implements Producer {
    private int width = 200;

    private int height = 50;

    public static void main(String[] args) {
        File file = new File("C:\\Users\\13522\\Desktop\\xs\\卷一-第一章 青云.txt");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            BufferedReader novelbr = new BufferedReader(inputStreamReader);
            int currentIndex = 1;
            String line = novelbr.readLine();
            List<String> lines = new ArrayList<>();
            while (line != null) {
                lines.add(line);
                line = novelbr.readLine();
            }
            novelbr.close();
            fileInputStream.close();
            createJieImage(1, lines);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        BufferedImage bi = kaptchaProducer().createImage("12234");
//        FileOutputStream out = null;
//        try {
//            out = new FileOutputStream(new File("C:\\Users\\13522\\Desktop\\test.jpg"));
//            ImageIO.write(bi, "jpg", out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (out != null) {
//                    out.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }

    public static Producer kaptchaProducer() {
        // 实例一个DefaultKaptcha
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        // 创建配置对象
        Properties properties = new Properties();
        // 设置边框
        properties.setProperty("kaptcha.border", "no");
        // 设置颜色
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 设置字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "blue");
        // 设置宽度
        properties.setProperty("kaptcha.image.width", "390");
        // 高度
        properties.setProperty("kaptcha.image.height", "50");
        // 设置session.key
        properties.setProperty("kaptcha.session.key", "code");
        // 设置文本长度
        properties.setProperty("kaptcha.textproducer.char.length", "6");
        // 设置字体
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        // 将以上属性设置为实例一个DefaultKaptcha的属性
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        // 将defaultKaptcha返回
        return defaultKaptcha;
    }

    static int tag1 = 0;
    static int i1 = 0;

    public static void createJieImage(int i, List<String> lines) {
        i1 = i;
        List<String> liness = new ArrayList<>();
        for (String line : lines) {
            handle(liness, line);
        }
        System.out.println(liness);
        int tag = 0;
        int linesize = 25;
        boolean flag = true;
        List<StringBuilder> sbs = new ArrayList<>();
        while (flag) {
            List<String> li = null;
            if (tag + linesize >= liness.size()) {
                li = liness.subList(tag, liness.size());
                flag = false;
            } else {
                li = liness.subList(tag, tag + linesize);
            }
            StringBuilder sb = new StringBuilder();
            for (String s : li) {
                sb.append(s).append("\r\n");
            }
            tag += linesize;
            sbs.add(sb);
        }
        tag1 = tag1++;
        for (StringBuilder sb : sbs) {
            BufferedImage bi = kaptchaProducer().createImage("123456");
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(new File("C:\\Users\\13522\\Desktop\\xs\\test_" + i1 + "_" + tag1 + ".png"));
                ImageIO.write(bi, "jpg", out);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void handle(List<String> liness, String line) {
        if (line.length() > 30) {
            liness.add(line.substring(0, 30));
            handle(liness, line.substring(30));
        } else {
            liness.add(line);
        }
    }

    /**
     * Create an image which will have written a distorted text.
     *
     * @param text the distorted characters
     * @return image with the text
     */
    public BufferedImage createImage(String text) {
        WordRenderer wordRenderer = getConfig().getWordRendererImpl();
        GimpyEngine gimpyEngine = getConfig().getObscurificatorImpl();
        BackgroundProducer backgroundProducer = getConfig().getBackgroundImpl();
        boolean isBorderDrawn = getConfig().isBorderDrawn();
        this.width = getConfig().getWidth();
        this.height = getConfig().getHeight();

        BufferedImage bi = wordRenderer.renderWord(text, width, height);
        bi = gimpyEngine.getDistortedImage(bi);
        bi = backgroundProducer.addBackground(bi);
        Graphics2D graphics = bi.createGraphics();
        if (isBorderDrawn) {
            drawBox(graphics);
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(new File("C:\\Users\\13522\\Desktop\\xs\\test_" + i1 + "_" + tag1 + ".png"));
            ImageIO.write(bi, "png", out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return bi;
    }

    @Override
    public String createText() {
        return null;
    }

    private void drawBox(Graphics2D graphics) {
        Color borderColor = getConfig().getBorderColor();
        int borderThickness = getConfig().getBorderThickness();

        graphics.setColor(borderColor);

        if (borderThickness != 1) {
            BasicStroke stroke = new BasicStroke((float) borderThickness);
            graphics.setStroke(stroke);
        }

        Line2D line1 = new Line2D.Double(0, 0, 0, width);
        graphics.draw(line1);
        Line2D line2 = new Line2D.Double(0, 0, width, 0);
        graphics.draw(line2);
        line2 = new Line2D.Double(0, height - 1, width, height - 1);
        graphics.draw(line2);
        line2 = new Line2D.Double(width - 1, height - 1, width - 1, 0);
        graphics.draw(line2);
    }
}
